<style lang="less">
@import "./carManage.less";
</style>
<template>
  <div class="search">
    <Row>
      <Col>
        <Card>
          <Row>
            <Form
              ref="searchForm"
              :model="searchForm"
              inline
              :label-width="100"
              class="search-form"
            >
              <Form-item label="车牌号">
                <Input
                  type="text"
                  v-model="searchForm.plateNum"
                  clearable
                  placeholder="车牌号"
                  style="width: 200px"
                />
              </Form-item>
              <span v-if="drop"></span>
              <Form-item style="margin-left:-35px;" class="br">
                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                <Button @click="handleReset">重置</Button>
                <a class="drop-down" @click="dropDown">
                  {{dropDownContent}}
                  <Icon :type="dropDownIcon"></Icon>
                </a>
              </Form-item>
            </Form>
          </Row>
          <Row class="operation">
            <Button @click="add" type="primary" icon="md-add" v-has="'add'">添加车辆</Button>
            <circleLoading v-if="operationLoading" />
          </Row>
          <!-- <Row>
          <Alert show-icon>
            已选择
            <span class="select-count">{{selectCount}}</span> 项
            <a class="select-clear" @click="clearSelectAll">清空</a>
          </Alert>
          </Row>-->
          <Row>
            <Table
              :loading="loading"
              max-height="600"
              border
              :columns="columns"
              :data="data"
              sortable="custom"
              @on-sort-change="changeSort"
              @on-selection-change="showSelect"
              ref="table"
            ></Table>
          </Row>
          <Row type="flex" justify="end" class="page">
            <Page
              :current="searchForm.current"
              :total="total"
              :page-size="searchForm.size"
              @on-change="changePage"
              @on-page-size-change="changePageSize"
              :page-size-opts="[10,20,50,100]"
              size="small"
              show-total
              show-elevator
              show-sizer
            ></Page>
          </Row>
        </Card>
      </Col>
    </Row>
    <!--新增编辑-->
    <carForm
      :modalShow="carModalVisible"
      :deviceTypes="deviceTypes"
      :modalType="modalType"
      :parentForm="carForm"
      @listenModalForm="hideModel"
      @refreshTableList="getCarList"
    ></carForm>
  </div>
</template>

<script>
import { plateColorSelect, carTypeSelect } from "@/libs/dict";
import {
  getCarPageList,
  addCar,
  editCar,
  deleteCar,
  getDevType
} from "@/api/operate";
import circleLoading from "../../my-components/circle-loading.vue";
import carForm from "./carForm";

export default {
  name: "car-manage",
  components: {
    carForm,
    circleLoading
  },
  data() {
    return {
      drop: false,
      dropDownContent: "展开",
      dropDownIcon: "ios-arrow-down",
      plateColorSelect: plateColorSelect,
      carTypeSelect: carTypeSelect,
      loading: true,
      operationLoading: false,
      selectCount: 0,
      selectList: [],
      searchForm: {
        plateNum: "",
        current: 1,
        size: 20,
        sort: "createTime",
        order: "desc"
      },
      modalType: 0,
      carModalVisible: false,
      modalTitle: "",
      carForm: {
        carId: "",
        clinetId: "",
        deviceId: "",
        deptId: "",
        plateNum: "",
        plateColor: "",
        carType: "",
        carPic: "",
        carBrand: "",
        remark: ""
      },
      submitLoading: false,
      columns: [
        // {
        //   type: 'selection',
        //   width: 60,
        //   align: 'center',
        //   fixed: 'left'
        // },
        {
          title: "序号",
          type: "index",
          width: 65,
          align: "center",
          fixed: "left"
        },
        {
          title: "车牌号",
          key: "plateNum",
          width: 180,
          sortable: true
        },
        {
          title: "车牌颜色",
          key: "plateColor",
          width: 180,
          render: (h, params) => {
            return h(
              "span",
              this.plateColorSelect.find(item => {
                return item.value == params.row.plateColor;
              }).label
            );
          }
        },
        {
          title: "车辆类型",
          key: "carType",
          width: 180,
          render: (h, params) => {
            return h(
              "span",
              this.carTypeSelect.find(item => {
                return item.value == params.row.carType;
              }).label
            );
          }
        },
        {
          title: "品牌",
          key: "carBrand",
          width: 180
        },
        {
          title: "设备卡号",
          key: "simCard",
          width: 180
        },
        {
          title: "设备IMEI",
          key: "imei",
          width: 180
        },
        {
          title: "备注",
          key: "remark",
          minWidth: 200
        },

        {
          title: "操作",
          key: "action",
          width: 180,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "warning",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  directives: [
                    {
                      name: "has",
                      value: "edit"
                    }
                  ],
                  on: {
                    click: () => {
                      this.edit(params.row);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  directives: [
                    {
                      name: "has",
                      value: "delete"
                    }
                  ],
                  on: {
                    click: () => {
                      this.remove(params.row);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      data: [],
      total: 0
    };
  },
  methods: {
    init() {
      this.getCarList();
    },
    changePage(v) {
      this.searchForm.current = v;
      this.getCarList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.size = v;
      this.getCarList();
    },
    getCarList() {
      // 多条件搜索
      this.loading = true;
      getCarPageList(this.searchForm).then(res => {
        this.loading = false;
        if (res.code == 0) {
          this.data = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    dropDown() {
      if (this.drop) {
        this.dropDownContent = "展开";
        this.dropDownIcon = "ios-arrow-down";
      } else {
        this.dropDownContent = "收起";
        this.dropDownIcon = "ios-arrow-up";
      }
      this.drop = !this.drop;
    },
    handleSearch() {
      this.searchForm.current = 1;
      this.searchForm.size = 20;
      this.getCarList();
    },
    handleReset() {
      this.searchForm.beanName = "";
      this.searchForm.current = 1;
      this.searchForm.size = 20;
      // 重新加载数据
      this.getCarList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == "normal") {
        this.searchForm.order = "";
      }
      this.getCarList();
    },

    add() {
      this.modalType = 0;
      Object.keys(this.carForm).forEach(key => (this.carForm[key] = ""));
      this.carModalVisible = true;
    },
    edit(v) {
      this.modalType = 1;
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = "";
        }
      }
      this.carForm = JSON.parse(JSON.stringify(v));
      this.carModalVisible = true;
    },
    remove(v) {
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除车辆 " + v.licnum + " ?",
        loading: true,
        onOk: () => {
          deleteCar(v.carId).then(res => {
            this.$Modal.remove();
            if (res.code == 0) {
              this.$Message.success("删除成功");
              this.getCarList();
            } else {
              this.$Message.error(res.msg);
            }
          });
        }
      });
    },
    showSelect(e) {
      this.exportData = e;
      this.selectList = e;
      this.selectCount = e.length;
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    delAll() {
      if (this.selectCount <= 0) {
        this.$Message.warning("您还未选择要删除的数据");
        return;
      }
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
        loading: true,
        onOk: () => {
          let ids = "";
          this.selectList.forEach(function(e) {
            ids += e.id + ",";
          });
          ids = ids.substring(0, ids.length - 1);
          deleteCar(ids).then(res => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("删除成功");
              this.clearSelectAll();
              this.getUserList();
            }
          });
        }
      });
    },
    hideModel() {
      this.carModalVisible = false;
    }
  },
  mounted() {
    this.init();
  }
};
</script>
